//
//  UITabBar+XYAdd.m
//  PeiJue
//
//  Created by lxy on 2017/9/28.
//  Copyright © 2017年 lixiya. All rights reserved.
//

#import "UITabBar+XYAdd.h"

static NSInteger const TabbarItemNums = 5;
static NSInteger const BadgeViewTag = 1000;

@implementation UITabBar (XYAdd)

- (void)xy_showBadgeOnItemIndex:(int)index{
    
    [self _removeBadgeOnItemIndex:index];
    
    UIView *badgeView = [[UIView alloc]init];
    badgeView.tag = BadgeViewTag + index;
    badgeView.backgroundColor = [UIColor redColor];
    
    CGRect tabFrame = self.frame;
    float percentX = (index +0.6) / TabbarItemNums;
    CGFloat x = ceilf(percentX * tabFrame.size.width) + 8;
    CGFloat y = ceilf(0.1 * tabFrame.size.height);
    
    badgeView.frame = CGRectMake(x, y, 8, 8);
    badgeView.layer.cornerRadius = badgeView.frame.size.width * 0.5;
    
    [self addSubview:badgeView];
    
}

- (void)xy_hideBadgeOnItemIndex:(int)index{
    
    [self _removeBadgeOnItemIndex:index];
    
}

- (void)_removeBadgeOnItemIndex:(int)index{
    
    for (UIView *subView in self.subviews) {
        
        if (subView.tag == BadgeViewTag+index) {
            [subView removeFromSuperview];
        }
    }
}


@end
